<?php

/**
 * WeUser.php
 *
 * @author: wjp 2017-04-29
 */
class Model_WeUser extends PhalApi_Model_NotORM
{
    /**
     * 根据主键值返回对应的表名，注意分表的情况
     */
    protected function getTableName($id)
    {
        return 'user';
    }

    /**
     * 重写getORM方法，方便下面使用
     */
    protected function getORM($id = NULL)
    {
        return parent::getORM($id == NULL ? 'we_base' : $id);
    }

//    public static $mtokeen;

    public function __construct()
    {
//        self::$mtoken = DI()->config->get('app.Pay.token');
    }


    /**
     * 获取用户信息
     * @param string $phone
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function get_logUserinfo($phone)
    {
        try {
            $sql = "select * from jc_user where phone = {$phone} && status = 1";
            $user_info = $this->getORM()->queryAll($sql);
            return isset($user_info[0])?$user_info[0]:-1;
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

    /**
     * 获取用户信息
     * @param int $id
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function getUserId($id)
    {
        try {
            $sql = "select * from jc_user where id = {$id}";
            $user_info = $this->getORM()->queryAll($sql);
            if (isset($user_info[0])) {
                return $user_info[0];
            } else {
                return -1;
            }
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

    /**
     * 获取用户信息
     * @param int $phone
     * @param string $pwd
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function getPhonePwd($phone,$pwd)
    {
        try {
            $sql = "select * from jc_user where phone = {$phone} && pwd1 = '{$pwd}'";
            $user_info = $this->getORM()->queryAll($sql);
            if (isset($user_info[0])) {
                return $user_info[0];
            } else {
                return -1;
            }
        } catch (Exception $ex) {
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }

    /**
     * 插入用户
     * @desc wjp 2017-04-29
     * @return array
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertUser($data)
    {
        try {
            $ret = $this->getORM()->insert($data);

            return isset($ret)?$ret:null;
        } catch (Exception $ex) {
            print_r(['trace' => __METHOD__, 'msg' => $ex->getMessage()]);exit;
            DI()->logger->error('SQL_ERROR', ['trace' => __METHOD__, 'msg' => $ex->getMessage()]);
            throw new PhalApi_Exception_InternalServerError(T('Request failed'));
        }
    }
}